package com.moli.iov.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @Auther: zql
 * @Date: 2018/12/21 9:43
 */
public class ConvertUtils<S, T> {

    private static final Logger logger = LoggerFactory.getLogger(ConvertUtils.class);

    public static<S, T> IPage<T> exchangePage(IPage<S> source, T t){
        if(source != null && source.getRecords() != null && !source.getRecords().isEmpty()){
            List<T> ts = new ArrayList<>();
            for(S s : source.getRecords()){
                try{
                    CommonUtils.copyProperties(s, t);
                    ts.add(t);
                }catch (Exception e){
                    logger.error(String.format("参数查询 分页 组装结果 异常 参数：%s", s), e);
                    return null;
                }
            }
            IPage<T>  page = new Page<T>();
            page.setRecords(ts);
            page.setTotal(source.getTotal());
            page.setPages(source.getPages());
            page.setSize(source.getSize());
            page.setCurrent(source.getCurrent());
            return page;
        }
        return null;
    }
}
